﻿namespace HuaWei.CommClass
{
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using System;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public static class ImageHelp
    {
        public static Image ByteArrayToImage(byte[] arr_byte)
        {
            Image image = null;
            if ((arr_byte == null) || (arr_byte.Length == 0))
            {
                return image;
            }
            MemoryStream stream = new MemoryStream(arr_byte);
            if (stream == null)
            {
                return image;
            }
            try
            {
                return Image.FromStream(stream);
            }
            catch
            {
                return null;
            }
        }

        public static Image BytToImg(byte[] byt)
        {
            MemoryStream stream = new MemoryStream(byt);
            return Image.FromStream(stream);
        }

        public static void CutForCustom(Stream streamFile, string fileSaveUrl, int maxWidth, int maxHeight, int quality)
        {
            Image image = Image.FromStream(streamFile, true);
            if ((image.Width <= maxWidth) && (image.Height <= maxHeight))
            {
                image.Save(fileSaveUrl, ImageFormat.Jpeg);
            }
            else
            {
                Image image2;
                Graphics graphics;
                double num = ((double) maxWidth) / ((double) maxHeight);
                double num2 = ((double) image.Width) / ((double) image.Height);
                if (num == num2)
                {
                    image2 = new Bitmap(maxWidth, maxHeight);
                    graphics = Graphics.FromImage(image2);
                    graphics.InterpolationMode = InterpolationMode.High;
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.Clear(Color.White);
                    graphics.DrawImage(image, new Rectangle(0, 0, maxWidth, maxHeight), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                    image2.Save(fileSaveUrl, ImageFormat.Jpeg);
                }
                else
                {
                    Image image3 = null;
                    Graphics graphics2 = null;
                    Rectangle srcRect = new Rectangle(0, 0, 0, 0);
                    Rectangle destRect = new Rectangle(0, 0, 0, 0);
                    if (num > num2)
                    {
                        image3 = new Bitmap(image.Width, (int) Math.Floor((double) (((double) image.Width) / num)));
                        graphics2 = Graphics.FromImage(image3);
                        srcRect.X = 0;
                        srcRect.Y = (int) Math.Floor((double) ((image.Height - (((double) image.Width) / num)) / 2.0));
                        srcRect.Width = image.Width;
                        srcRect.Height = (int) Math.Floor((double) (((double) image.Width) / num));
                        destRect.X = 0;
                        destRect.Y = 0;
                        destRect.Width = image.Width;
                        destRect.Height = (int) Math.Floor((double) (((double) image.Width) / num));
                    }
                    else
                    {
                        image3 = new Bitmap((int) Math.Floor((double) (image.Height * num)), image.Height);
                        graphics2 = Graphics.FromImage(image3);
                        srcRect.X = (int) Math.Floor((double) ((image.Width - (image.Height * num)) / 2.0));
                        srcRect.Y = 0;
                        srcRect.Width = (int) Math.Floor((double) (image.Height * num));
                        srcRect.Height = image.Height;
                        destRect.X = 0;
                        destRect.Y = 0;
                        destRect.Width = (int) Math.Floor((double) (image.Height * num));
                        destRect.Height = image.Height;
                    }
                    graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics2.SmoothingMode = SmoothingMode.HighQuality;
                    graphics2.DrawImage(image, destRect, srcRect, GraphicsUnit.Pixel);
                    image2 = new Bitmap(maxWidth, maxHeight);
                    graphics = Graphics.FromImage(image2);
                    graphics.InterpolationMode = InterpolationMode.High;
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.Clear(Color.White);
                    graphics.DrawImage(image3, new Rectangle(0, 0, maxWidth, maxHeight), new Rectangle(0, 0, image3.Width, image3.Height), GraphicsUnit.Pixel);
                    ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                    ImageCodecInfo encoder = null;
                    foreach (ImageCodecInfo info2 in imageEncoders)
                    {
                        if ((((info2.MimeType == "image/jpeg") || (info2.MimeType == "image/bmp")) || (info2.MimeType == "image/png")) || (info2.MimeType == "image/gif"))
                        {
                            encoder = info2;
                        }
                    }
                    EncoderParameters encoderParams = new EncoderParameters(1);
                    encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long) quality);
                    image2.Save(fileSaveUrl, encoder, encoderParams);
                    graphics.Dispose();
                    image2.Dispose();
                    graphics2.Dispose();
                    image3.Dispose();
                }
            }
            image.Dispose();
        }

        public static void CutForSquare(Stream fromFile, string fileSaveUrl, int side, int quality)
        {
            string directoryName = Path.GetDirectoryName(fileSaveUrl);
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            Image image = Image.FromStream(fromFile, true);
            if ((image.Width <= side) && (image.Height <= side))
            {
                image.Save(fileSaveUrl, ImageFormat.Jpeg);
            }
            else
            {
                int width = image.Width;
                int height = image.Height;
                if (width != height)
                {
                    Rectangle rectangle;
                    Rectangle rectangle2;
                    Image image2 = null;
                    Graphics graphics = null;
                    if (width > height)
                    {
                        image2 = new Bitmap(height, height);
                        graphics = Graphics.FromImage(image2);
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        rectangle = new Rectangle((width - height) / 2, 0, height, height);
                        rectangle2 = new Rectangle(0, 0, height, height);
                        graphics.DrawImage(image, rectangle2, rectangle, GraphicsUnit.Pixel);
                        width = height;
                    }
                    else
                    {
                        image2 = new Bitmap(width, width);
                        graphics = Graphics.FromImage(image2);
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
                        rectangle = new Rectangle(0, (height - width) / 2, width, width);
                        rectangle2 = new Rectangle(0, 0, width, width);
                        graphics.DrawImage(image, rectangle2, rectangle, GraphicsUnit.Pixel);
                        height = width;
                    }
                    image = (Image) image2.Clone();
                    graphics.Dispose();
                    image2.Dispose();
                }
                Image image3 = new Bitmap(side, side);
                Graphics graphics2 = Graphics.FromImage(image3);
                graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics2.SmoothingMode = SmoothingMode.HighQuality;
                graphics2.Clear(Color.White);
                graphics2.DrawImage(image, new Rectangle(0, 0, side, side), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
                ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo encoder = null;
                foreach (ImageCodecInfo info2 in imageEncoders)
                {
                    if ((((info2.MimeType == "image/jpeg") || (info2.MimeType == "image/bmp")) || (info2.MimeType == "image/png")) || (info2.MimeType == "image/gif"))
                    {
                        encoder = info2;
                    }
                }
                EncoderParameters encoderParams = new EncoderParameters(1);
                encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long) quality);
                image3.Save(fileSaveUrl, encoder, encoderParams);
                encoderParams.Dispose();
                graphics2.Dispose();
                image3.Dispose();
                image.Dispose();
            }
        }

        public static void DelImage(DataRow row, string col_file, string col_filename, PictureEdit picedit)
        {
            row.BeginEdit();
            row[col_file] = new byte[0];
            row[col_filename] = "";
            row.EndEdit();
            if (picedit != null)
            {
                picedit.Properties.SizeMode = PictureSizeMode.Zoom;
                picedit.Image = null;
            }
        }

        public static void ExportImage(DataRow row, string col_file, string col_filename)
        {
            string filename = row[col_filename].ToString();
            byte[] buffer = (row[col_file] == DBNull.Value) ? null : ((byte[]) row[col_file]);
            SaveFile(filename, buffer);
        }

        public static byte[] ImgToByt(Image img)
        {
            MemoryStream stream = new MemoryStream();
            img.Save(stream, ImageFormat.Jpeg);
            return stream.GetBuffer();
        }

        public static void ImportImage(DataRow row, string col_file, string col_filename, PictureEdit picedit)
        {
            string str;
            byte[] buffer = OpenFile(out str);
            row.BeginEdit();
            row[col_file] = buffer;
            row[col_filename] = Path.GetFileName(str);
            row.EndEdit();
            if (picedit != null)
            {
                Image image = ByteArrayToImage(buffer);
                picedit.Properties.SizeMode = PictureSizeMode.Zoom;
                picedit.Image = image;
            }
        }

        public static bool IsWebImage(string contentType)
        {
            return ((((contentType == "image/pjpeg") || (contentType == "image/jpeg")) || ((contentType == "image/gif") || (contentType == "image/bmp"))) || (contentType == "image/png"));
        }

        public static byte[] OpenFile(out string path_file)
        {
            path_file = "";
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "jpg|*.jpg|bmp|*.bmp";
            byte[] buffer = null;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (Stream stream = dialog.OpenFile())
                {
                    if (stream != null)
                    {
                        path_file = dialog.FileName;
                        int length = (int) stream.Length;
                        buffer = new byte[length];
                        stream.Read(buffer, 0, length);
                        stream.Close();
                    }
                }
            }
            return buffer;
        }

        public static void SaveFile(string filename, byte[] arr_byte)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.FileName = filename;
            if ((dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) && ((arr_byte != null) && (arr_byte.Length != 0)))
            {
                using (Stream stream = dialog.OpenFile())
                {
                    if (stream != null)
                    {
                        stream.Write(arr_byte, 0, arr_byte.Length);
                        stream.Close();
                    }
                }
            }
        }

        public static void ShowImage(DataRow row, string col_file, PictureEdit picedit)
        {
            byte[] buffer;
            if (row == null)
            {
                buffer = null;
            }
            else
            {
                buffer = (row[col_file] == DBNull.Value) ? null : ((byte[]) row[col_file]);
            }
            Image image = ByteArrayToImage(buffer);
            picedit.Properties.SizeMode = PictureSizeMode.Zoom;
            picedit.Image = image;
        }

        private static byte[] smethod(string str)
        {
            FileStream stream = new FileStream(str, FileMode.Open);
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            stream.Close();
            return buffer;
        }

        public static Image ZoomAuto(Stream postedFile, double targetWidth, double targetHeight, string watermarkText, string watermarkImage)
        {
            Graphics graphics;
            Font font;
            Brush brush;
            Image image2;
            ImageAttributes attributes;
            ColorMap map;
            ColorMap[] mapArray2;
            float[][] numArray;
            float[] numArray2;
            float[][] numArray3;
            ColorMatrix matrix;
            Image image = Image.FromStream(postedFile, true);
            if ((image.Width <= targetWidth) && (image.Height <= targetHeight))
            {
                if (watermarkText != "")
                {
                    using (graphics = Graphics.FromImage(image))
                    {
                        font = new Font("黑体", 10f);
                        brush = new SolidBrush(Color.White);
                        graphics.DrawString(watermarkText, font, brush, (float) 10f, (float) 10f);
                        graphics.Dispose();
                    }
                }
                if ((watermarkImage != "") && File.Exists(watermarkImage))
                {
                    using (image2 = Image.FromFile(watermarkImage))
                    {
                        if ((image.Width >= image2.Width) && (image.Height >= image2.Height))
                        {
                            graphics = Graphics.FromImage(image);
                            attributes = new ImageAttributes();
                            map = new ColorMap();
                            map.OldColor = Color.FromArgb(0xff, 0, 0xff, 0);
                            map.NewColor = Color.FromArgb(0, 0, 0, 0);
                            mapArray2 = new ColorMap[] { map };
                            attributes.SetRemapTable(mapArray2, ColorAdjustType.Bitmap);
                            numArray = new float[5][];
                            numArray2 = new float[5];
                            numArray2[0] = 1f;
                            numArray[0] = numArray2;
                            numArray2 = new float[5];
                            numArray2[1] = 1f;
                            numArray[1] = numArray2;
                            numArray2 = new float[5];
                            numArray2[2] = 1f;
                            numArray[2] = numArray2;
                            numArray2 = new float[5];
                            numArray2[3] = 0.5f;
                            numArray[3] = numArray2;
                            numArray2 = new float[5];
                            numArray2[4] = 1f;
                            numArray[4] = numArray2;
                            numArray3 = numArray;
                            matrix = new ColorMatrix(numArray3);
                            attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                            graphics.DrawImage(image2, new Rectangle(image.Width - image2.Width, image.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, attributes);
                            graphics.Dispose();
                        }
                        image2.Dispose();
                    }
                }
                return image;
            }
            double width = image.Width;
            double height = image.Height;
            if ((image.Width > image.Height) || (image.Width == image.Height))
            {
                if (image.Width > targetWidth)
                {
                    width = targetWidth;
                    height = image.Height * (targetWidth / ((double) image.Width));
                }
            }
            else if (image.Height > targetHeight)
            {
                height = targetHeight;
                width = image.Width * (targetHeight / ((double) image.Height));
            }
            Image image4 = new Bitmap((int) width, (int) height);
            Graphics graphics2 = Graphics.FromImage(image4);
            graphics2.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics2.SmoothingMode = SmoothingMode.HighQuality;
            graphics2.Clear(Color.White);
            graphics2.DrawImage(image, new Rectangle(0, 0, image4.Width, image4.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
            if (watermarkText != "")
            {
                using (graphics = Graphics.FromImage(image4))
                {
                    font = new Font("宋体", 10f);
                    brush = new SolidBrush(Color.White);
                    graphics.DrawString(watermarkText, font, brush, (float) 10f, (float) 10f);
                    graphics.Dispose();
                }
            }
            if ((watermarkImage != "") && File.Exists(watermarkImage))
            {
                using (image2 = Image.FromFile(watermarkImage))
                {
                    if ((image4.Width >= image2.Width) && (image4.Height >= image2.Height))
                    {
                        graphics = Graphics.FromImage(image4);
                        attributes = new ImageAttributes();
                        map = new ColorMap();
                        map.OldColor = Color.FromArgb(0xff, 0, 0xff, 0);
                        map.NewColor = Color.FromArgb(0, 0, 0, 0);
                        mapArray2 = new ColorMap[] { map };
                        attributes.SetRemapTable(mapArray2, ColorAdjustType.Bitmap);
                        numArray = new float[5][];
                        numArray2 = new float[5];
                        numArray2[0] = 1f;
                        numArray[0] = numArray2;
                        numArray2 = new float[5];
                        numArray2[1] = 1f;
                        numArray[1] = numArray2;
                        numArray2 = new float[5];
                        numArray2[2] = 1f;
                        numArray[2] = numArray2;
                        numArray2 = new float[5];
                        numArray2[3] = 0.5f;
                        numArray[3] = numArray2;
                        numArray2 = new float[5];
                        numArray2[4] = 1f;
                        numArray[4] = numArray2;
                        numArray3 = numArray;
                        matrix = new ColorMatrix(numArray3);
                        attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                        graphics.DrawImage(image2, new Rectangle(image4.Width - image2.Width, image4.Height - image2.Height, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, attributes);
                        graphics.Dispose();
                    }
                    image2.Dispose();
                }
            }
            return image4;
        }
    }
}

